package senior.ioExer2;

import java.io.*;

/**
 * 获得文件行数的方法
 *         有 bufferedReader.readLine 方法 或者查找byte是否=='\n',比较谁快！
 *
 * 运行结果：
 *      （FileInputStream）行数：94行,用时：23200ms
 *      （bufferedReader）行数：95行,用时：888ms
 *
 *      为什么两种方式读取的行数不同？
 *      （FileInputStream）最后一行没有"\n"
 *
 * @author shkstart
 * @creat 2022-04-19:18
 */
public class Exer7 {
    public static void main(String[] args) {
        File file = new File("D:/.temp/gaowu.txt");

        try {
            long start1 = System.nanoTime();
            FileInputStream fis = new FileInputStream(file);
            int length = 0;
            int len;
            while ((len = fis.read()) != -1){
                if(len == '\n'){
                    length++;
                }

            }

            long end1 = System.nanoTime();
            System.out.println("行数：" + length + "行,用时：" + (end1 - start1)/1000 + "ms");
            fis.close();
            long start2 = System.nanoTime();
            BufferedReader br = new BufferedReader(new FileReader(file));
            int length1 = 0;
            while (br.readLine() != null){
                length1++;
            }
            long end2 = System.nanoTime();
            System.out.println("行数：" + length1 + "行,用时：" + (end2 - start2)/1000 + "ms");
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
